package org.example;

import org.example.dto.BookCatalogueDto;
import org.jsoup.Jsoup;
import org.jsoup.internal.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Hello world!
 */
public class App {
	private static Pattern pattern=Pattern.compile("<a\\s*href=\"?([\\w\\W]*?)\"?[\\s]*?[^>]>([\\s\\S]*?)(?=</a>)");

	public static void main(String[] args) {
/*		try {
			test();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		download();
	}

	public static void download() {
		try {
			String bookCod="34_34660";
			Document document=Jsoup.connect("https://www.ddyueshu.com/34_34660").get();
			Element info=document.getElementById("info");
			String bookName=info.select("h1").text();
//			String bookName="我真不是盖世高人";
			String path="/usr/local/webapps/file/" + bookName + ".txt";
			File file=new File(path);
        /*    if(file.exists()){
                file.mkdirs();
            }*/
			DownloadBook.setDataSource("xbiquke");
			List<BookCatalogueDto> bookCatalogue=DownloadBook.getBookCatalogue(bookCod, document, pattern);
			System.out.println("---------------" + bookName + "正在下载" + "--------------");
			DownloadBook.downloadBook(bookCod, bookName, bookCatalogue);
			System.out.println("---------------" + bookName + "下载完成" + "--------------");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    public static void test() throws IOException {
        Document document=Jsoup.connect("https://www.ddyueshu.com/34_34660/7877834.html").get();
//		Element info=document.getElementById("info");
//		String bookName=info.select("h1").text();
		Elements chapterName = document.select("h1");
		Elements elements = document.select("#content");
		List<TextNode> textNodes=elements.textNodes();
		for (TextNode node:textNodes) {
			String text=node.text();
			if(text==null||text.trim().length()==0){
				continue;
			}
			System.out.println(text);
		}




    }
}
